


var EXPORTED_SYMBOLS = ["Dialogs"];




let Dialogs = (function() {
    "use strict";
    const Cu = Components.utils;
    const Ci = Components.interfaces;
    const Cc = Components.classes;
    function require(url) {
        let scope = {};
        Cu.import(url, scope);
        return scope;
    }
    let {MiscUtils} = require("chrome://jsm/content/utils.js");
    let {Prefs} = require("chrome://jsm/content/Preferences.js");
    let {Services} = require("resource://gre/modules/Services.jsm");
    let {FileUtils} = require("resource://gre/modules/FileUtils.jsm");

    return {
        
        browseForFolder: function (title, start_dir) {
            return new Promise((resolve, reject) => {
                let fp = Cc["@mozilla.org/filepicker;1"]
                    .createInstance(Ci.nsIFilePicker);
                fp.init(MiscUtils.browserWindow, title, 
                        Ci.nsIFilePicker.modeGetFolder);
                if (start_dir)
                    fp.displayDirectory = new FileUtils.File(start_dir);
                if (fp.show() == Ci.nsIFilePicker.returnOK) {
                    resolve(fp.file.path);
                } else {
                    resolve(null);
                }
            });
        },

        browseForFileSave: function (title, filename, start_dir) {
            return new Promise((resolve, reject) => {
                var fp = Cc["@mozilla.org/filepicker;1"]
                    .createInstance(Ci.nsIFilePicker);
                fp.init(MiscUtils.browserWindow, title, 
                        Ci.nsIFilePicker.modeSave);
                fp.defaultString = filename;
                
                if (/\.js$i/.test(filename))
                    fp.appendFilter("iMacros script", "*.js");
                else if (/\.iim$/i.test(filename))
                    fp.appendFilter("iMacros macro", "*.iim");
                else if (/\.(?:png|jpe?g)$/i.test(filename))
                    fp.appendFilters(fp.filterImages);
                fp.appendFilters(Ci.nsIFilePicker.filterAll);

                fp.filterIndex = 0;
                let start = start_dir || Prefs.defaultMacroPath;
                fp.displayDirectory = new FileUtils.File(start);
                
                let r = fp.show();
                if(r == Ci.nsIFilePicker.returnOK ||
                   r == Ci.nsIFilePicker.returnReplace) {
                    resolve(fp.file.path);
                } else {
                    resolve(null);
                }
            });
        },


        browseForFileOpen: function (title, start_dir) {
            return new Promise((resolve, reject) => {
                let fp = Cc["@mozilla.org/filepicker;1"]
                    .createInstance(Ci.nsIFilePicker);
                fp.init(MiscUtils.browserWindow, title, 
                        Ci.nsIFilePicker.modeOpen);
                fp.appendFilters(Ci.nsIFilePicker.filterAll);
                fp.filterIndex = 0;
                let start = start_dir || Prefs.defaultMacroPath;
                fp.displayDirectory = new FileUtils.File(start);
                let r = fp.show();
                if(r == Ci.nsIFilePicker.returnOK ||
                   r == Ci.nsIFilePicker.returnReplace) {
                    resolve(fp.file.path);
                } else {
                    resolve(null);
                }
            });
        },

        
        confirm: function(text) {
            return new Promise((resolve, reject) => {
                let check = {value: false};
                let flags = Services.prompt.STD_YES_NO_BUTTONS;
                let button = Services.prompt.confirmEx(
                    MiscUtils.browserWindow, "", text,
                    flags, "", "", "", null, check
                );
                resolve(button == 0);
            });
        },

        confirmCheck: function(title, msg, check_msg, check_value) {
            return new Promise((resolve, reject) => {
                let check = {value: false};
                let flags = Services.prompt.STD_YES_NO_BUTTONS;
                let button = Services.prompt.confirmEx(
                    MiscUtils.browserWindow, title, msg,
                    flags, "", "", "", check_msg, check_value
                );
                resolve(button == 0);
            });
        },

        prompt: function(title, msg, input, check_msg, check) {
            return new Promise((resolve, reject) => {
                resolve(Services.prompt.prompt(
                    MiscUtils.browserWindow, title, msg, input, check_msg, check
                ));
            });
        }
    };
})();
